const { join } = require('path')

const alias = require('@rollup/plugin-alias')
const commonjs = require('@rollup/plugin-commonjs')
const resolve = require('@rollup/plugin-node-resolve').default // rollup --exports 默认auto, export出来的从1个变成多个会有问题
const strip = require('@rollup/plugin-strip')
const replace = require('rollup-plugin-replace')

const {
  CDN_HOST,
  SLARDAR_WEB_CDN_HOST,
  DOMAIN_HOST,
  CDN_PATH,
  REPORT_BATCH_PATH,
  SDK_DOMAIN,
  SDK_BATCH_PATH,
  SDK_SLARDAR_PLUGINS_PATH,
  LITE_CDN_PATH,
  SDK_SLARDAR_ELECTRON_CRASH_DOMAIN,
  SDK_SLARDAR_ELECTRON_CRASH_PATH,
} = require('./region')

const getPath = ({ packageJson, region }) => {
  const isDevelopment = process.env.NODE_ENV !== 'production'

  const slardarPluginsHost = isDevelopment ? 'localhost:8080' : CDN_HOST[region]
  const slardarDomainHost = DOMAIN_HOST[region]
  const reportBatchPath = REPORT_BATCH_PATH[region]

  const slardarPluginsProtocol = isDevelopment ? 'http:' : 'https:'
  const slardarPluginsPath = isDevelopment ? `/${region}/plugins` : CDN_PATH[region]
  const slardarLitePluginsPath = isDevelopment ? `/${region}/plugins` : LITE_CDN_PATH[region]
  const sentry = isDevelopment ? `sentry.${region}.js` : `sentry.${packageJson.version}.${region}.js`
  const behavior = isDevelopment ? `behavior.${region}.js` : `behavior.${packageJson.version}.${region}.js`
  const worker = isDevelopment ? `worker.${region}.js` : `worker.${packageJson.version}.${region}.js`
  const monitors = isDevelopment ? `monitors.${region}.js` : `monitors.${packageJson.version}.${region}.js`
  const heatmap = isDevelopment ? `heatmap.${region}.js` : `heatmap.${packageJson.version}.${region}.js`
  const heatmapDraw = isDevelopment ? `heatmap-draw.${region}.js` : `heatmap-draw.${packageJson.version}.${region}.js`
  const heatmapMouseDraw = isDevelopment
    ? `heatmap-mouse-draw.${region}.js`
    : `heatmap-mouse-draw.${packageJson.version}.${region}.js`
  const heatmapScrollDraw = isDevelopment
    ? `heatmap-scroll-draw.${region}.js`
    : `heatmap-scroll-draw.${packageJson.version}.${region}.js`

  // sdk-slardar paths
  const sdkSlardarReportDomain = SDK_DOMAIN[region]
  const sdkSlardarReportBatchPath = SDK_BATCH_PATH[region]
  const sdkSlardarPluginsProtocol = isDevelopment ? 'http:' : 'https:'
  const sdkSlardarPluginsHost = isDevelopment ? 'localhost:8080' : SLARDAR_WEB_CDN_HOST[region]
  const sdkSlardarPluginsPath =
    isDevelopment && !process.env.UPLOAD_CDN ? `/${region}/plugins` : SDK_SLARDAR_PLUGINS_PATH[region]
  const sdkSlardarElectronCrashDomain = SDK_SLARDAR_ELECTRON_CRASH_DOMAIN[region]
  const sdkSlardarElectronClashPath = SDK_SLARDAR_ELECTRON_CRASH_PATH

  return {
    slardarDomainHost,
    slardarPluginsHost,
    sentry,
    behavior,
    worker,
    monitors,
    heatmap,
    heatmapDraw,
    heatmapMouseDraw,
    heatmapScrollDraw,
    slardarPluginsPath,
    slardarLitePluginsPath,
    slardarPluginsProtocol,
    isDevelopment,
    reportBatchPath,

    sdkSlardarReportDomain,
    sdkSlardarReportBatchPath,
    sdkSlardarPluginsProtocol,
    sdkSlardarPluginsHost,
    sdkSlardarPluginsPath,
    sdkSlardarElectronCrashDomain,
    sdkSlardarElectronClashPath,
  }
}

const packagesRoot = join(__dirname, '../../../', 'packages')
const aliasConfig = alias({
  entries: [
    { find: /^@slardar\/sdk$/, replacement: join(packagesRoot, 'browser', 'esm') },
    { find: /^@slardar\/sdk-core$/, replacement: join(packagesRoot, 'core', 'esm') },
    { find: /^@slardar\/trace$/, replacement: join(packagesRoot, 'trace', 'esm') },
    { find: /^@slardar\/polyfill$/, replacement: join(packagesRoot, 'polyfill') },
    { find: /^@slardar\/types$/, replacement: join(packagesRoot, 'types', 'esm') },
    { find: /^@slardar\/sdk-web$/, replacement: join(packagesRoot, 'sdk-web', 'esm', 'index.js') },
    { find: /^@slardar\/web$/, replacement: join(packagesRoot, 'web', 'esm', 'exports.js') },
    { find: /^@apmplus\/web$/, replacement: join(packagesRoot, 'apmplus-web', 'esm', 'exports.js') },
    { find: /^@slardar\/sdk-template$/, replacement: join(packagesRoot, 'sdk-template', 'esm') },
    { find: /^@slardar\/base$/, replacement: join(packagesRoot, 'base', 'esm') },
    { find: /^@slardar\/web-vitals$/, replacement: join(packagesRoot, 'web-vitals', 'dist', 'web-vitals.js') },
    {
      find: /^@slardar\/web-vitals\/base$/,
      replacement: join(packagesRoot, 'web-vitals', 'dist', 'web-vitals.base.js'),
    },
    // mini
    {
      find: /^@slardar\/sdk-mini-program$/,
      replacement: join(packagesRoot, 'sdk-mini-program', 'esm', 'index.js'),
    },
    {
      find: /^@slardar\/sdk-mini-program\/test$/,
      replacement: join(packagesRoot, 'sdk-mini-program', 'esm', 'test', 'index.js'),
    },
    {
      find: /^@slardar\/rx$/,
      replacement: join(packagesRoot, 'rx', 'esm', 'index.js'),
    },
  ],
})

function getReplaceConfig({ packageJson, region, clientMode, sdkName }) {
  const {
    slardarDomainHost,
    slardarPluginsHost,
    sentry,
    behavior,
    worker,
    monitors,
    heatmap,
    heatmapDraw,
    heatmapMouseDraw,
    heatmapScrollDraw,
    slardarPluginsPath,
    slardarLitePluginsPath,
    slardarPluginsProtocol,
    isDevelopment,
    reportBatchPath,
    sdkSlardarReportDomain,
    sdkSlardarReportBatchPath,
    sdkSlardarPluginsProtocol,
    sdkSlardarPluginsHost,
    sdkSlardarPluginsPath,
    sdkSlardarElectronCrashDomain,
    sdkSlardarElectronClashPath,
  } = getPath({
    packageJson,
    region,
  })

  return {
    MONITOR_SDK_VERSION: packageJson.version,
    SLARDAR_VERSION: packageJson.version,
    'process.env.SDK_VERSION': JSON.stringify(packageJson.version),
    SLARDAR_REGION: region,
    'process.env.SDK_NAME': JSON.stringify(sdkName || ''),
    'process.env.REPORT_DOMAIN': JSON.stringify(slardarDomainHost),
    'process.env.REPORT_BATCH_PATH': JSON.stringify(reportBatchPath),
    'process.env.SLARDAR_DOMAIN_HOST': JSON.stringify(slardarDomainHost),
    'process.env.SLARDAR_PLUGINS_HOST': JSON.stringify(slardarPluginsHost),
    'process.env.SLARDAR_SENTRY_LINK': JSON.stringify(sentry),
    'process.env.SLARDAR_BEHAVIOR_LINK': JSON.stringify(behavior),
    'process.env.SLARDAR_WORKER_LINK': JSON.stringify(worker),
    'process.env.SLARDAR_MONITORS_LINK': JSON.stringify(monitors),
    'process.env.SLARDAR_HEATMAP_LINK': JSON.stringify(heatmap),
    'process.env.SLARDAR_HEATMAP_DRAW_LINK': JSON.stringify(heatmapDraw),
    'process.env.SLARDAR_HEATMAP_MOUSE_DRAW_LINK': JSON.stringify(heatmapMouseDraw),
    'process.env.SLARDAR_HEATMAP_SCROLL_DRAW_LINK': JSON.stringify(heatmapScrollDraw),
    'process.env.SLARDAR_PLUGINS_PATH': JSON.stringify(slardarPluginsPath),
    'process.env.SLARDAR_LITE_PLUGINS_PATH': JSON.stringify(slardarLitePluginsPath),
    'process.env.SLARDAR_PLUGINS_PROTOCOL': JSON.stringify(slardarPluginsProtocol),
    'process.env.NODE_ENV': JSON.stringify(isDevelopment ? 'development' : 'production'),
    'process.env.CLIENT_MODE': JSON.stringify(clientMode ? clientMode : 'browser'),

    'process.env.SDK_SLARDAR_VERSION': JSON.stringify(packageJson.version),
    'process.env.SDK_SLARDAR_REPORT_DOMAIN': JSON.stringify(sdkSlardarReportDomain),
    'process.env.SDK_SLARDAR_REPORT_BATCH_PATH': JSON.stringify(sdkSlardarReportBatchPath),
    'process.env.SDK_SLARDAR_PLUGINS_PROTOCOL': JSON.stringify(sdkSlardarPluginsProtocol),
    'process.env.SDK_SLARDAR_PLUGINS_HOST': JSON.stringify(sdkSlardarPluginsHost),
    'process.env.SDK_SLARDAR_PLUGINS_PATH': JSON.stringify(sdkSlardarPluginsPath),
    'process.env.UPLOAD_CDN': process.env.UPLOAD_CDN,
    'process.env.SDK_SLARDAR_ELECTRON_CRASH_DOMAIN': JSON.stringify(sdkSlardarElectronCrashDomain),
    'process.env.SDK_SLARDAR_ELECTRON_CRASH_PATH': JSON.stringify(sdkSlardarElectronClashPath),

    'process.env.DEFAULT_BATCH_WAIT': process.env.CYPRESS_BUILD ? '100' : '1000',
  }
}

function getBasicPlugins(configs) {
  const replaceConfig = getReplaceConfig(configs)
  return [
    aliasConfig,
    replace(replaceConfig),
    commonjs(),
    resolve(),
    configs.isDevelopment
      ? null
      : strip({
          functions: ['log', 'debug'],
          labels: ['debug', 'benchmark'],
        }),
  ]
}

module.exports = {
  aliasConfig,
  getReplaceConfig,
  getBasicPlugins,
  getPath,
}
